package com.glory.wxmemsicpda.activity

import android.view.View
import androidx.databinding.DataBindingUtil
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.adapter.CheckDescListAdapter
import com.glory.wxmemsicpda.base.BaseDialogActivity
import com.glory.wxmemsicpda.bean.CheckSheetBean
import com.glory.wxmemsicpda.bean.EAPCheckBean
import com.glory.wxmemsicpda.bean.SheetInfoBean
import com.glory.wxmemsicpda.bean.net.BaseResponseBean
import com.glory.wxmemsicpda.databinding.ActivityCheckDetailsBinding
import com.glory.wxmemsicpda.utils.NetUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import com.lzy.okgo.model.Response
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_SimpleDialogEntityCallBack
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils
import kotlinx.android.synthetic.main.activity_check_details.*
import org.json.JSONObject
import java.util.*

/**
 * 点检作业
 */
class CheckDetailsActivity : BaseDialogActivity(), View.OnClickListener {
    private var mData: CheckSheetBean? = null

    private var mAdapter: CheckDescListAdapter? = null
    private var mObjectRRN = ""
    private var mCheckTime: String? = null
    private var mCheckTimeList: MutableList<String>? = null
    private var mCheckTimePop: FreeUI_GeneralPop? = null
    private var viewDataBinding: ActivityCheckDetailsBinding? = null

    override fun getLayoutResId(): Int {
        return 0
    }

    override fun initView() {
        viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_check_details)

        setTitle("点检表单详情")

        freeUI_titleView.setRightListener("提交") {
            //region 测试
//            showSubmitDialog(object : onSubmitListener {
//                override fun setOnSubmitListener() {
//                    showConfirmDialog(this@CheckDetailsActivity, "提交成功")
//                }
//
//            })
            //endregion
            updateCheckSheet()
        }

        initRecyclerView(rvCheckDescList, this)
        flCheckTime.setOnClickListener(this)

        val bundle = intent.extras
        mData = bundle!!.getSerializable("SHEETDATA") as CheckSheetBean?
        mObjectRRN = intent.extras!!.getString("OBJECTRRN")!!
        if (mObjectRRN == null) mObjectRRN = ""
        mCheckTime = intent.extras!!.getString("CHECKTIME")
        tvResult.visibility = if (mCheckTime == "巡检") View.VISIBLE else View.GONE
        tvComment.visibility = if (mCheckTime == "巡检") View.VISIBLE else View.GONE
        llCheckTime!!.visibility = if (mCheckTime == "巡检") View.GONE else View.VISIBLE
        val sheetchecktablelines: MutableList<SheetInfoBean.SheetCheckTableLineEntity?>? =
            mData!!.sheetlist[0].sheetchecktablelines

        //region 测试
//        mAdapter = CheckDescListAdapter(
//            if (mCheckTime == "巡检") R.layout.adapter_check_desc_inspection else R.layout.adapter_check_desc,
//            sheetchecktablelines,
//            mCheckTime,
//            this
//        )
        //endregion
        mAdapter = CheckDescListAdapter(
            if (mCheckTime == "巡检") R.layout.adapter_check_desc_inspection else R.layout.adapter_check_desc,
            getCheckData(sheetchecktablelines),
            mCheckTime,
            this
        )
        rvCheckDescList.adapter = mAdapter

        mCheckTimeList = mutableListOf()
        mCheckTimeList?.add("调机点检")
        mCheckTimeList?.add("改机点检")
    }

    override fun initData() {
        viewDataBinding?.checkSheetBean = mData
        viewDataBinding?.mObjectRRN = mObjectRRN
    }

    override fun getBaseEntity(): FreeUI_AddViewEntity {
        return FreeUI_AddViewEntity("header", llMain)
    }

    //region 接口调用
    //获取参数值
    private fun getCheckData(sheetchecktablelines: MutableList<SheetInfoBean.SheetCheckTableLineEntity?>?): MutableList<SheetInfoBean.SheetCheckTableLineEntity?>? {
        val mEapData: MutableList<SheetInfoBean.SheetCheckTableLineEntity> =
            ArrayList<SheetInfoBean.SheetCheckTableLineEntity>()
        val iterator: MutableIterator<SheetInfoBean.SheetCheckTableLineEntity> =
            sheetchecktablelines?.iterator() as MutableIterator<SheetInfoBean.SheetCheckTableLineEntity>
        while (iterator.hasNext()) {
            val next: SheetInfoBean.SheetCheckTableLineEntity = iterator.next()
            if (!next.reserveD2.equals("")) {
                mEapData.add(next)
                iterator.remove()
            }
        }
        if (mEapData.size > 0) {
            val checkList: MutableList<EAPCheckBean.CheckBean> =
                ArrayList<EAPCheckBean.CheckBean>()
            for (entity in mEapData) {
                val checkEntity: EAPCheckBean.CheckBean =
                    EAPCheckBean.CheckBean(
                        entity.reserveD2
                    )
                checkList.add(checkEntity)
            }

            val map = JsonObject()
            map.addProperty("EQPID", tvEqpID.text.toString())
            map.add("PARAMTERLIST", JsonParser().parse(Gson().toJson(checkList)).asJsonArray)
            NetUtils.requestMesPosNet(
                this,
                "GetEqpCurrentParamter",
                "CPC.GetEqpCurrentParamter",
                map,
                object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<EAPCheckBean?>?>(this) {
                    override fun onSuccess(response: Response<BaseResponseBean<EAPCheckBean?>?>) {
                        super.onSuccess(response)
                        if (response.body()!!.isSuccess(true, supportFragmentManager)
                        ) {
                            val paramterlist: List<EAPCheckBean.CheckBean> =
                                response.body()!!.response.body!!.paramterlist
                            if (paramterlist.isEmpty()) {
                                showConfirmDialog("EAP返回参数信息为空!")
                                return
                            }
                            for (entity in mEapData) {
                                for (checkEntity in paramterlist) {
                                    if (entity.reserveD2.equals(checkEntity.name)) {
                                        entity.value = checkEntity.value
                                    }
                                }
                            }
                            sheetchecktablelines.addAll(mEapData)
                            mAdapter?.setNewData(sheetchecktablelines)
                            return
                        }
                    }

                    override fun onError(response: Response<BaseResponseBean<EAPCheckBean?>?>) {
                        super.onError(response)
                        loadError(response.exception, "GetEqpCurrentParamter")
                    }
                })
        }
        return sheetchecktablelines
    }

    //提交
    private fun updateCheckSheet() {
        showSubmitDialog(object : onSubmitListener {
            override fun setOnSubmitListener() {

                val map = JsonObject()
                map.addProperty("ACTIONTYPE", "SAVE")
                map.addProperty("CHECKSHEETDATARRN", mObjectRRN)
                val sheetInfoEntity: SheetInfoBean = mData!!.sheetlist[0]
                sheetInfoEntity.sheetchecktablelines = mAdapter?.data
                sheetInfoEntity.grouP3 = tvCheckTime.text.toString()
                map.add(
                    "SHEETLIST",
                    JsonParser().parse(Gson().toJson(sheetInfoEntity)).asJsonObject
                )
                NetUtils.requestMesPosNet(
                    this,
                    "CHECKSHEET",
                    "MEMSIC.CHECKSHEET",
                    map,
                    object :
                        FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<JSONObject?>?>(this@CheckDetailsActivity) {
                        override fun onSuccess(response: Response<BaseResponseBean<JSONObject?>?>) {
                            super.onSuccess(response)
                            if (response.body()
                                !!.isSuccess(false, supportFragmentManager)
                            ) {
                                showConfirmDialog(this@CheckDetailsActivity, "提交成功")
                            }
                        }

                        override fun onError(response: Response<BaseResponseBean<JSONObject?>?>) {
                            super.onError(response)
                            loadError(response.exception, "CHECKSHEET")
                        }
                    })
            }

        })
    }

    //endregion
    override fun onClick(v: View?) {
        if (v?.id == R.id.flCheckTime) {
            if (mCheckTimePop != null) {
                if (mCheckTimePop!!.isShowing) {
                    mCheckTimePop!!.dismiss()
                }
            } else {
                mCheckTimePop = FreeApi_PopUtils.getNormalPop(
                    applicationContext, flCheckTime.width, mCheckTimeList, ivCheckTime
                ) { codeS: String?, i: Int ->
                    tvCheckTime.text = codeS
                }
            }
            FreeApi_PopUtils.setArrowShow(ivCheckTime)
            mCheckTimePop!!.showAsDropDown(flCheckTime, 0, 0)
        }
    }
}